//
//  PicCollectionViewController.m
//  01 微博
//
//  Created by zhoup on 16/5/27.
//  Copyright © 2016年 zhoup. All rights reserved.
//

#import "PicCollectionViewController.h"
#import "PicCollectionViewCell.h"


@interface PicCollectionViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>



@property(nonatomic,strong) UIImagePickerController *pickVc;
@end

@implementation PicCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

-(NSMutableArray *)images
{
    if (!_images) {
        _images = [NSMutableArray arrayWithCapacity:4];
    }
    return _images;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1.注册通知监听item的添加和删除通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addPhoto) name:KAddPicFormPhoto object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deletePhoto:) name:KDeletePicFormPhoto object:nil];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)addPhoto
{
    UIImagePickerController *pickVc = [[UIImagePickerController alloc]init];
    pickVc.delegate = self;
    self.pickVc = pickVc;
    [self presentViewController:pickVc animated:YES completion:nil];
    
}

-(void)deletePhoto:(NSNotification *)note
{
    if (self.images.count == 0) {
        return;
    }
    PicCollectionViewCell *cell =  note.object;
    
    NSIndexPath *indexpath = cell.indexPath ;
    
    [self.images removeObjectAtIndex:indexpath.item];
    
    [self.collectionView reloadData];
}
#pragma mark - imagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    //选中图片
    UIImage *image =  info[UIImagePickerControllerOriginalImage];
    
    [self.images addObject:image];
    
    [self.collectionView reloadData];
    
    [self.pickVc dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    //关闭
    [self.pickVc dismissViewControllerAnimated:YES completion:nil];
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    //多出一个用来增加相册的相片
    return self.images.count + 1;
}

- (PicCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    if (indexPath.item == self.images.count)
    {
        //最后一张
         cell.iconImage = nil;
    }
    else
    {
        cell.indexPath = indexPath;
        cell.iconImage = self.images[indexPath.item];
    }
   
    return cell;
}




@end
